package com.system.blogAdmin.blog.controller;

import com.cnc.common.web.ezPage.EzPage;
import com.cnc.common.web.util.MessageBox;
import com.system.blogAdmin.blog.dto.CategoryDto;
import com.system.blogAdmin.blog.service.BlogCategoryService;
import com.system.blogAdmin.auth.entity.User;
import com.system.blogAdmin.blog.entity.BlogCategory;
import com.system.blogAdmin.utils.DtoUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 作者： cnc
 * 创建时间：2017-06-22.
 * 版本：1.0
 */
@Controller
@RequestMapping(value = "/blogCategory")
public class BlogCategoryController {

    @Autowired
    BlogCategoryService blogCategoryService;

    @RequestMapping(value = "/index",method = RequestMethod.GET)
    public String index(){
        return "blogAdmin/blog/blog_category";
    }

    @RequestMapping(value = "/list",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
    @ResponseBody
    public EzPage<CategoryDto> getListBlog(){
        Map<String , Object> map = new HashMap<String , Object>();
        List<CategoryDto> treeDtoList = DtoUtils.categoryToDto(blogCategoryService.listBy(map));

        for (CategoryDto categoryDto : treeDtoList) {
            categoryDto.setName(categoryDto.getName());
        }

        EzPage<CategoryDto> rs = new EzPage<CategoryDto>();
        rs.setRows(treeDtoList);
        rs.setTotal(treeDtoList == null ? 0 : treeDtoList.size());
        return  rs;
    }

    @RequestMapping(value = "/listCategoryTree",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    @ResponseBody
    public List<BlogCategory> getListCategory(){
        List<BlogCategory> blogCategory = blogCategoryService.listBy(new HashMap<String , Object>());
        for (BlogCategory category : blogCategory) {
            category.setName(category.getName());
        }
        return  blogCategory;
    }


    @RequestMapping(value = "/add", method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    @ResponseBody
    public MessageBox addCategory(BlogCategory blogCategory){
        Map<String ,Object> map = new HashMap<String , Object>();
        blogCategory.setName(blogCategory.getName());
        map.put("name",blogCategory.getName());
        List<BlogCategory> blogCategoryList = blogCategoryService.listBy(map);
        if(blogCategoryList.size()>0){
            return MessageBox.getErrorMsg("类名重复！");
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddhhmmss");
        blogCategory.setCreateTime(simpleDateFormat.format(new Date()));
        blogCategoryService.insertCategory(blogCategory);
        return MessageBox.getSuccessMsg("添加成功！");
    }

    @RequestMapping(value = "/update" , method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    @ResponseBody
    public MessageBox updateCategory(BlogCategory blogCategory){
        Map<String ,Object> map = new HashMap<String , Object>();

        blogCategory.setName(blogCategory.getName());

        map.put("name",blogCategory.getName());
        List<BlogCategory> blogCategoryList = blogCategoryService.listBy(map);

        if(blogCategoryList.size()>0){
            return MessageBox.getErrorMsg("类名重复！");
        }

        Subject subject = SecurityUtils.getSubject();
        User u = (User) subject.getPrincipal();
        blogCategory.setCreateId(Integer.parseInt(u.getId().toString()));
        blogCategoryService.update(blogCategory);
        return MessageBox.getSuccessMsg("添加成功！");
    }
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    @ResponseBody
    public MessageBox deleteCategory(@PathVariable Long id){
        Map<String ,Object> map = new HashMap<String , Object>();
        map.put("pid",id);
        List<BlogCategory> blogCategoryList = blogCategoryService.listBy(map);
        if(blogCategoryList.size()>0){
            return MessageBox.getErrorMsg("有下级菜单无法删除！");
        }
        blogCategoryService.deleteById(id);
        return MessageBox.getSuccessMsg("删除成功！");
    }
}
